import React from "react";

class Trunk extends React.Component {
  constructor (props) {
    super(props)
    this.growTimes = (props.growTimes ?? 5000) / (props.speed ?? 1)
    this.state = {
      // 树干初始长度
      height: 1,
      ...props
    }
    // 检查是否停止生长字段
    this.maxHeight = 0
  }
  componentDidMount () {
    this.timer = setInterval(this.trunkGrow, this.growTimes)
  }
  componentWillUnmount () {
    clearInterval(this.timer)
  }
  // 树干生长
  trunkGrow = () => {
    console.log('树干长大了')
  }
  // 检查是否停止生长
  isStopGrowing = (that) => {
    let { nodeEle } = that
    let clinetHeight = Number(getComputedStyle(nodeEle ?? this.nodeEle).height.split('px')[0])
    if (clinetHeight > this.maxHeight) {
      this.maxHeight = clinetHeight
      console.log(nodeEle, false)
      return false
    } else {
      console.log(nodeEle, true)
      return true
    }
  }
  // 点击可以修剪
  cut = (event) => {
    event.stopPropagation()
    this.nodeEle.parentElement.removeChild(this.nodeEle)
  }
  // render () {
  //   return <div className="Trunk"></div>
  // }
}
export default Trunk;